package com.ruge.test.springMVC.定时任务调度.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

/**
 * 爱丽丝、如歌  创建于 2018/6/22 15:26
 * 说明:  SimpleTrigger 的学习
 */
public class HelloScheduler2 {
    public static void main(String[] args) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(HelloJobBase.class).withIdentity("my job1", "group1").usingJobData("message", "hello Myjob1").usingJobData("floatJobValue", 3.14F).build();
        Date date = new Date();
        /**
         * 距离当前四秒钟后执行 仅执行一次任务
         */
        //SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger().startAt(new Date(date.getTime()+4000)).build();
        /**
         * 距离当前四秒钟后首次执行 之后每隔两秒钟重复执行
         */
        //SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger().startAt(new Date(date.getTime()+4000)).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(2000).repeatForever()).build();
        /**
         * 距离当前四秒钟后首次执行 之后执行三次
         */
        //SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger().startAt(new Date(date.getTime()+4000)).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(2000).withRepeatCount(3)).build();
        /**
         * 距离当前四秒钟后首次执行 距离当前二十秒钟后停止执行
         */
        SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger().startAt(new Date(date.getTime()+4000)).endAt(new Date(date.getTime()+20000)).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(2000).repeatForever()).build();
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
        scheduler.scheduleJob(jobDetail, trigger);
    }
}
